From f90ec98c509fad7146c169d8d762b4c847d07af8 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 15 Sep 2023 09:15:54 +0700
Subject: Add product context provider

---
 src/pages/shop/product/[slug].jsx | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

(limited to 'src/pages/shop/product/[slug].jsx')

diff --git a/src/pages/shop/product/[slug].jsx b/src/pages/shop/product/[slug].jsx
index af20413f..534aa8da 100644
--- a/src/pages/shop/product/[slug].jsx
+++ b/src/pages/shop/product/[slug].jsx
@@ -7,6 +7,8 @@ import dynamic from 'next/dynamic'
 import { useRouter } from 'next/router'
 import cookie from 'cookie'
 import axios from 'axios'
+import { useProductContext } from '@/contexts/ProductContext'
+import { useEffect } from 'react'
 
 const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))
 const Product = dynamic(() => import('@/lib/product/components/Product/Product'))
@@ -19,7 +21,7 @@ export async function getServerSideProps(context) {
   const authToken = auth?.token || ''
 
   let response = await axios(
-    `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=`+getIdFromSlug(slug)
+    `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=` + getIdFromSlug(slug)
   )
   let product = response.data
   // let productSolr = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } })
@@ -41,8 +43,15 @@ export async function getServerSideProps(context) {
   }
 }
 
-export default function ProductDetail({ product}) {
+export default function ProductDetail({ product }) {
   const router = useRouter()
+  const { setProduct } = useProductContext()
+
+  useEffect(() => {
+    if (product) {
+      setProduct(product)
+    }
+  }, [product, setProduct])
 
   if (!product) return <PageNotFound />
 
-- 
cgit v1.2.3

